package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.mapper.ClueTrackRecordMapper;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private ClueTrackRecordMapper clueTrackRecordMapper;
    @PostMapping
    public Result addClue(@RequestBody Clue clue) {
        log.info("添加线索");
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success();
    }

    /**
     * 分页列表查询
     */
    @RequestMapping
    public Result listClues(ClueDto clueDto) {
        log.info("分页列表查询: clueDto={}", clueDto);
        PageResult<Clue> pageResult = clueService.list(clueDto);
        return Result.success(pageResult);

    }

    /**
     * 线索分配
     */
    @Log
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assignClue(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("线索分配: clueId={}, userId={}", clueId, userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询线索: {}", id);
        Clue clue =clueService.getByClueId(id);
        return Result.success(clue);
    }
    /**
     * 线索跟进
     */
    @Log
    @PutMapping
    public Result trackClue(@RequestBody Clue clue) {
        log.info("线索跟进: {}", clue);
        clueService.trackClue(clue);
        return Result.success();

    }

    /**
     * 伪线索处理
     */
    @Log
    @PutMapping("/false/{id}")
    public Result falseClue(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto) {
        log.info("伪线索处理: {}", id);
        clueService.markFalseClue(id, markFalseClueDto);
        return Result.success();


    }



    /**
     * 转商机
     */
    @Log
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("转商机: {}", id);
        clueService.toBusiness(id);
        return Result.success();
    }

    /**
     * 线索池列表查询
     */
    @GetMapping("/pool")
    public Result listPoolClues(ClueDto clueDto) {
        log.info("线索池列表查询: {}", clueDto);
        PageResult<Clue> pageResult = clueService.listPoolClues(clueDto);
        return Result.success(pageResult);
    }
}
